package com.way.filemanager.util;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.way.filemanager.R;
import com.way.filemanager.util.FileDeleteDialog.LocalHandler;

import java.io.File;
import java.util.LinkedList;
import java.util.Locale;

import hwdroid.dialog.AlertDialog;
import hwdroid.dialog.Dialog;
import hwdroid.dialog.DialogInterface;

public class FileDetailDialog {
    Activity mActivity;
    FileRenameDialog_OnResultCb mOnResultCb;
    Dialog mDialog;
    EditText mEditText;
    LocalHandler mLocalHandler;
    Thread mThread;
    long mFileSize = 0;
    long systemtime = System.currentTimeMillis();
    boolean bIsForceStop;
    TextView sizeTextView;
    public FileDetailDialog(Activity m, int titleResId, FileInfo filename) {
        mActivity = m;
        final FileInfo file = filename;
        LayoutInflater factory = LayoutInflater.from(mActivity);
        View detailView = factory.inflate(R.layout.alert_dialog_text_entry_detail, null);
        
        TextView pathTextView =  (TextView)detailView.findViewById(R.id.path_context);
        sizeTextView =  (TextView)detailView.findViewById(R.id.size_context);
        TextView timeTextView =  (TextView)detailView.findViewById(R.id.time_context);
        TextView readableTextView =  (TextView)detailView.findViewById(R.id.readable_context);
        TextView writeableTextView =  (TextView)detailView.findViewById(R.id.writeable_context);
        TextView hideTextView =  (TextView)detailView.findViewById(R.id.hide_context);
        
        pathTextView.setText(file.GetFilePath());
        sizeTextView.setText(CommonFunc.FormetFileSize(file.GetFileSize()));
        timeTextView.setText(CommonFunc.CreateTimeFormat(file.GetModifyTime()));
        if(file.GetFileSystemObject().getPermissions().mGroup.isRead())
            readableTextView.setText(R.string.yes);
        else
            readableTextView.setText(R.string.no);
        
        if(file.GetFileSystemObject().getPermissions().mGroup.isWrite())
            writeableTextView.setText(R.string.yes);
        else
            writeableTextView.setText(R.string.no);
        
        if(file.GetFileSystemObject().isHidden())
            hideTextView.setText(R.string.yes);
        else
            hideTextView.setText(R.string.no);
        
        mDialog = new AlertDialog.Builder(mActivity)
                .setTitle(file.GetFileName())
                .setView(detailView)
                .setNegativeButton(R.string.ali_iknow, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        /* User clicked cancel so do some stuff */
                        bIsForceStop = true;
                    }
                }).create();
        mThread = new Thread(new Runnable()
        {
            @Override
            public void run() {
                listFilesSize(new File(file.GetFullPath()),mFileSize);
                if(!bIsForceStop)
                    mLocalHandler.sendEmptyMessage(1);
            }
        });
        
    }

    public void show(FileRenameDialog_OnResultCb cb) {
        mOnResultCb = cb;
        mLocalHandler = new LocalHandler();
        mDialog.show();
        systemtime = System.currentTimeMillis();
        bIsForceStop = false;
        mThread.start();
    }

    public interface FileRenameDialog_OnResultCb {
        /**
         * @param Res :Result code
         * @param r :fullpath after renamed
         */
        int Resultcb(int Res, String r);
    }
    long listFilesSize(File file,long size)
    {
        if(bIsForceStop)
            return size;
        if(file.isDirectory())
        {
            File[] files = file.listFiles();
            if(files != null && files.length > 0)
            {
                for(File f : files)
                {
                    if(f.isFile())
                    {
                        size += f.length();
                    }
                    else if(f.isDirectory())
                    {
                        size = listFilesSize(f, size);
                    }
                }
            }
        }
        else
            size += file.length();
        mFileSize = size;
        if((System.currentTimeMillis() - systemtime)>100)
        {
            mLocalHandler.removeMessages(1);
            mLocalHandler.sendEmptyMessage(1);
            systemtime = System.currentTimeMillis();
        }
        return size;
    }

    @SuppressLint("HandlerLeak")
    public class LocalHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    sizeTextView.setText(CommonFunc.FormetFileSize(mFileSize));
                    break;
                default:
                    break;
            }
        }
    }
}
